<?php

namespace app\api\controller;

use app\admin\model\group\GroupTaboo;
use app\api\model\group\GroupCategory;
use app\api\model\group\GroupMedia;
use app\api\model\group\GroupNotice as GroupNoticeModel;
use app\api\model\group\GroupProject;
use \app\api\model\group\Group as GroupModel;
use \app\api\model\group\GroupEvaluate;
use app\api\model\group\GroupProjectApply;

use app\common\controller\Api;
use think\Db;

class Group extends Api
{
    protected $noNeedLogin = ['index','recommend','lists','data','category'];
    protected $noNeedRight = ['*'];

    //抱团首页的点赞/转发/评论/收藏数
    public function data()
    {
        $zan_num = GroupModel::sum('zan_num');
        $collect_num = GroupModel::sum('collect_num');
        $repost_num = GroupModel::sum('repost_num');
        $comment_num = GroupEvaluate::count();

        $data = [
            'zan_num'=>$zan_num,
            'collect_num'=>$collect_num,
            'repost_num'=>$repost_num,
            'comment_num'=>$comment_num
        ];

        $this->success('',$data);
    }

    //抱团列表
    public function lists()
    {
        $category_id = input('category_id',0);
        $is_recommend = input('is_recommend',0);
        $keyword = input('keyword','');

        $min_people = input('min_people',0);
        $max_people = input('max_people',0);
        $user_level = input('user_level',0);
        $team_gender = input('team_gender','all');
        $province_id = input('province_id',0);
        $city_id = input('city_id',0);
        $district_id = input('district_id',0);
        $taboo_id = input('taboo_id',0);
        $where = ['g.status'=>1];
        if ($province_id){
            $where['g.activity_province_id'] = $province_id;
        }
        /*if ($city_id){
            $where['g.activity_city_id'] = $city_id;
        }*/
        if ($district_id){
            $where['g.activity_district_id'] = $district_id;
        }
        if ($category_id){
            $where['pa.category_id'] = $category_id;
        }
        if ($is_recommend){
            $where['pa.is_recommend'] = 1;
        }
        if ($min_people&&$max_people){
            $where['g.people_num'] = ['between', [$min_people, $max_people]];
        }
        if(!empty($keyword)){
            $where['pa.name|g.nickname|g.title'] = ['like',"%$keyword%"];
        }
        if ($user_level){
            $where[] = ['exp',Db::raw("FIND_IN_SET('$user_level',g.user_level)")];
        }
        if ($team_gender!='all'){
            $where['g.team_gender'] = $team_gender;
        }
        if ($taboo_id){
            $where[] = ['exp',Db::raw("FIND_IN_SET('$taboo_id',g.taboo_id)")];
        }
        $project = GroupProjectApply::alias('pa')
            ->join('group g','pa.group_id = g.group_id AND g.deletetime IS NULL')
            ->where($where)
            ->field('pa.category_id,pa.is_recommend,pa.name as project_name,g.group_id,g.standard_img as image,g.nickname,g.realname,g.title,g.name,g.people_num,
            g.profile,g.user_level,g.team_gender,g.taboo_id,g.activity_province_id,g.activity_city_id,g.activity_district_id')
            ->order('g.zan_num desc')
            ->paginate(10, false, ['query' => request()->get()]);
       /* foreach ($project as $k=>$v){
            $service_img = GroupMedia::where(['group_id'=>$v['group_id'],'type'=>1])->find();
            $project[$k]['image'] = $service_img?$service_img['media']:'';
        }*/
        $this->success('',$project);
    }


    //增加抱团的转发次数
    public function addrepost()
    {
        $group_id = input('group_id',0);
        GroupModel::where(['group_id'=>$group_id])->setInc('repost_num',1);
        $this->success('');
    }

    //首页抱团
    public function index()
    {
        $city_id = input('city_id',0);
        $category_list = GroupCategory::field('id,name')->order('weigh desc')->select();
        foreach ($category_list as $k=>$v){
            $where = ['pa.category_id'=>$v['id'],'g.status'=>1,'pa.is_recommend'=>0];
            /*if ($city_id){
                $where['g.activity_city_id'] = $city_id;
            }*/
            $project = GroupProjectApply::alias('pa')
                ->join('group g','pa.group_id = g.group_id AND g.deletetime IS NULL')
                //->join('order o', 'u.id=o.user_id AND o.delete_time IS NULL')
                ->where($where)
                ->field('pa.category_id,pa.is_recommend,pa.name as project_name,g.group_id,g.standard_img as image,g.nickname,g.realname,g.title,g.name,g.people_num,
            g.profile,g.user_level,g.team_gender,g.taboo_id,g.activity_province_id,g.activity_city_id,g.activity_district_id')
                ->order('g.zan_num desc')
                ->limit(6)
                ->select();
            /*foreach ($project as $kk=>$vv){
                $service_img = GroupMedia::where(['group_id'=>$vv['group_id'],'type'=>1])->find();
                $project[$kk]['image'] = $service_img?$service_img['media']:'';
            }*/
            $category_list[$k]['project'] = $project;
        }

        $this->success('',$category_list);
    }


    //首页推荐抱团
    public function recommend()
    {
        $city_id = input('city_id',0);
        $where = ['pa.is_recommend'=>1,'g.status'=>1];
        if ($city_id){
            //$where['g.city_id'] = $city_id;
        }

        $project = GroupProjectApply::alias('pa')
            ->join('group g','pa.group_id = g.group_id AND g.deletetime IS NULL')
            ->where($where)
            ->field('pa.category_id,pa.is_recommend,pa.name as project_name,g.group_id,g.standard_img as image,g.nickname,g.realname,g.title,g.name,g.people_num,
            g.profile,g.user_level,g.team_gender,g.taboo_id,g.activity_province_id,g.activity_city_id,g.activity_district_id')
            //->order('g.zan_num desc')
            ->order('pa.is_top desc pa.is_recommend desc')
            ->limit(6)
            ->select();
        /*foreach ($project as $kk=>$vv){
            $service_img = GroupMedia::where(['group_id'=>$vv['group_id'],'type'=>1])->find();
            $project[$kk]['image'] = $service_img?$service_img['media']:'';
        }*/
        $this->success('',$project);
    }

    //抱团详情
    public function info()
    {
        $group_id = input('group_id',0);
        $info = GroupModel::where(['group_id'=>$group_id])->field('group_id,user_id,name,people_num,min_age,max_age,
        team_gender,user_level,activity_province,activity_city,activity_district,group_notice,taboo_id,explain,nickname,
        title,image,notice,zan_num,collect_num,repost_num,cover,is_activity_unlimited,hobby')->find();

        $group_project = GroupProjectApply::where(['group_id'=>$group_id])->field('name as project_name')->find();
        $info['project_name'] = $group_project['project_name'];
        $notice = GroupNoticeModel::where(['group_id'=>$group_id])->order('createtime desc')->find();
        if ($notice)$info['notice'] = $notice['notice'];

        $taboo_list = \app\admin\model\group\GroupTaboo::where('id','in',$info->taboo_id)->column('name');
        $info['taboo_list'] = implode('/',$taboo_list);

        $category_id =  GroupProjectApply::where(['group_id'=>$group_id])->group('category_id')->column('category_id');
        $category_list = GroupCategory::where('id','in',$category_id)->column('name');
        $info['category_name'] = implode('/',$category_list);

        /*$user_level = explode(',',$info->user_level);
        $user_level_list = [];
        $level_list = userlevel();
        foreach ($level_list as $k=>$v){
            if (in_array($k,$user_level)){
                array_push($user_level_list,$v);
            }
        }
        $info['user_level'] = implode('/',$user_level_list);*/
        if ($info['user_level']=='0'){
            $info['user_level'] = '不限';
        } else {
            $user_level_arr = [];
            $user_level = explode(',',$info['user_level']);
            foreach ($user_level as $k=>$v){
                if ($v==1){
                    array_push($user_level_arr,'普享会员');
                } elseif ($v==2){
                    array_push($user_level_arr,'惠享会员');
                } elseif ($v==3){
                    array_push($user_level_arr,'乐享会员');
                } elseif ($v==4){
                    array_push($user_level_arr,'优享会员');
                } elseif ($v==5){
                    array_push($user_level_arr,'尊享会员');
                }
            }
            $info['user_level'] = implode('/',$user_level_arr);
        }

        $info['comment_num'] = GroupEvaluate::where(['group_id'=>$group_id])->count();
        $cert_list = GroupMedia::where(['group_id'=>$group_id,'type'=>0])->limit(4)->select();
        $cert_count = GroupMedia::where(['group_id'=>$group_id,'type'=>0])->count();
        $info['cert_list'] = $cert_list;
        $info['cert_count'] = $cert_count;
        $image_list = GroupMedia::where(['group_id'=>$group_id,'type'=>1])->limit(4)->select();
        $image_count = GroupMedia::where(['group_id'=>$group_id,'type'=>1])->count();
        $info['image_list'] = $image_list;
        $info['image_count'] = $image_count;
        $video_list = GroupMedia::where(['group_id'=>$group_id,'type'=>2])->limit(4)->select();
        $video_count = GroupMedia::where(['group_id'=>$group_id,'type'=>2])->count();
        $info['video_list'] = $video_list;
        $info['video_count'] = $video_count;

        $this->success('',$info);
    }

    //团长信息
    public function headInfo()
    {
        $group_id = input('group_id',0);
        /*$info = GroupModel::where(['group_id'=>$group_id])->field('user_id,group_id,nickname,realname,gender,age,education,
        major,title,mobile,province,city,district,image,profile')->find();*/
        $info = GroupModel::where(['group_id'=>$group_id])
            ->field('group_id,user_id,realname,age,mobile,major,hobby,image,gender,education,nickname,city,profile as personal_intro,
            company_name,business_licence_number,company_found_date,company_address,company_attr, company_industry,company_business,
            company_people_num,company_mobile,company_intro,is_activity_unlimited')
            ->find();
        $user = \app\api\model\User::where(['id'=>$info['user_id']])
            ->field('id,level,auth_status,company_auth_status,level_type,person_auth,company_auth')->find();

        $user_model = new \app\api\model\User();

        $label = $user_model->getLevelLabelTextAttr($user);
        $user['level_label'] = $label;
        $level_text = $user_model->getLevelList();
        if ($user['level']>0){
            $user['level_text'] = $user['level']>0?$level_text[$user['level']]:'';
        } else {
            if ($user['person_auth']!=1&&$user['company_auth']!=1){
                $user['level_text'] = '阅览会员';
            } else{
                $user['level_text'] = '互动会员';
            }
        }
        $info['user_info'] = $user;
        if (!$user)$this->error('团长不存在');
        /*$user_info = \app\api\model\User::where(['id'=>$info['user_id']])
            ->field('id,nickname,company_name,business_licence_number,company_found_date,company_address,company_attr,
                company_industry,company_business,company_people_num,company_mobile,company_intro,personal_intro,hobby,mobile,
                city,education,realname,gender,job,birthday')
            ->find();*/
        if ($user['company_auth']==1){
            $info['is_company'] = 1;
        } else {
            //$user_info['age'] = $info['age'];
            //$user_info['age'] = calculateAge($user_info['birthday']);
            //$info['hobby'] = $user['hobby'];
            //$info['job'] = $user['job'];
            $info['is_company'] = 0;
        }
        //$user_info['image'] = $info['image'];
        $this->success('',$info);
    }

    //抱团的资质证书/图片/视频
    public function media()
    {
        $group_id = input('group_id',0);

        $cert_list = GroupMedia::where(['group_id'=>$group_id,'type'=>0])->select();
        $info['cert_list'] = $cert_list;
        $image_list = GroupMedia::where(['group_id'=>$group_id,'type'=>1])->select();
        $info['image_list'] = $image_list;
        $video_list = GroupMedia::where(['group_id'=>$group_id,'type'=>2])->select();
        $info['video_list'] = $video_list;

        $this->success('',$info);
    }


    //组团项目
    public function project()
    {

        $category_list = GroupCategory::field('id,name')->order('weigh desc')->select();
        foreach ($category_list as $k=>$v){
            $category_list[$k]['project'] = GroupProject::where(['category_id'=>$v['id']])
                ->field('id,category_id,name')->order('weigh desc')->select();
        }

        $this->success('',$category_list);
    }

    //禁忌列表
    public function taboo()
    {
        $list = GroupTaboo::field('id,name')->order('weigh desc')->select();
        $this->success('',$list);
    }

    //发起抱团申请
    public function apply()
    {
        try {
            $model = new GroupModel();
            $param = input();
            $param['user_id'] = $this->auth->id;
            $res = $model->apply($param);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('提交成功',$res);
    }

    //编辑抱团
    public function edit()
    {
        try {
            $model = new GroupModel();
            $param = input();
            $param['user_id'] = $this->auth->id;
            $res = $model->edit($param);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('提交成功',$res);
    }



    //判断身份是否可发起抱团
    public function can()
    {
        $can_group_level = \app\common\model\Config::get(['name'=>'can_group_level'])->value;
        $user = \app\api\model\User::get($this->auth->id);

        if ($can_group_level==0){
            $can = 1;
        } else {
            if ($user->level==$can_group_level){
                $can = 1;
            } else {
                $can = 0;
            }
        }

        $this->success('',$can);
    }

    public function category()
    {
        $category_list = GroupCategory::field('id,name,image')->order('weigh desc')->select();
        $this->success('',$category_list);
    }
}